<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 16/8/20
 * Time: 下午2:26
 */

namespace app\app\controller;


use think\Config;
use think\Controller;
use think\Request;

class User extends Controller
{
    public function test(){
        $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
        return ['data'=>$data,'code'=>1,'message'=>'操作完成'];
    }

    public function test1(){
        return array();
    }


    /**
     *
     */
    public function login(){
        $mobile = Request::instance()->post('mobile');
        $password = Request::instance()->post('password');
        $user = new \app\app\model\User();
        $rs = $user::get(['mobile'=>$mobile,'password'=>md5($password)]);
        if ($rs){
            return $rs;
        }else{
            return 1002;
        }
    }
    /**
     * 注册
     */
    public function register(){
        $user = new \app\app\model\User();

        $mobile = Request::instance()->post('mobile');
        $password = Request::instance()->post('password');
        $nick = Request::instance()->post('nick');
        $icon = Request::instance()->post('icon');
        $sex = Request::instance()->post('sex');
        $qqopenid = Request::instance()->post('qqopenid');
        $wbopenid = Request::instance()->post('wbopenid');
        $wxopenid = Request::instance()->post('wxopenid');

        if (!$user->verifyMoble($mobile)){
            return 1001;
        }

        $mobile && $user->mobile = $mobile;
        $password && $user->password = md5($password);
        $nick && $user->nick = $nick;
        $icon && $user->icon = $icon;
        $sex && $user->sex = $sex;
        $qqopenid && $user->qqopenid = $qqopenid;
        $wbopenid && $user->wbopenid = $wbopenid;
        $wxopenid && $user->wxopenid = $wxopenid;

        if ($user->save()){
            return array();
        }else{
            return 100;
        }
    }

    /**
     * 更新用户信息
     */
    public function updateUser(){
        $user = new \app\app\model\User();

        $id = Request::instance()->post('id');
        if ($id){
            $user = $user::get($id);
        }

        $mobile = Request::instance()->post('mobile');
        $password = Request::instance()->post('password');
        $nick = Request::instance()->post('nick');//昵称
        $icon = "";
//        $icon = Request::instance()->post('icon');//头像
        $sex = Request::instance()->post('sex');//性别

        if ($file = Request::instance()->file('icon')){
            $path = ROOT_PATH."public".DS."uploads".DS."icons";
            $info = $file->move($path);
            if ($info){
                $icon = DS."uploads".DS."icons".DS.date("Ymd").DS.$info->getFilename();
            }
        }

        $qqopenid = Request::instance()->post('qqopenid');
        $wbopenid = Request::instance()->post('wbopenid');
        $wxopenid = Request::instance()->post('wxopenid');

        //身高
        $height = Request::instance()->post('height');
        //年龄
        $age = Request::instance()->post('age');
        //体重
        $weight = Request::instance()->post('weight');

        if (!$user->verifyMoble($mobile)){
            return 1001;
        }

        $mobile && $user->mobile = $mobile;
        $password && $user->password = md5($password);
        $nick && $user->nick = $nick;
        $icon && $user->icon = $icon;
        $sex && $user->sex = $sex;
        $qqopenid && $user->qqopenid = $qqopenid;
        $wbopenid && $user->wbopenid = $wbopenid;
        $wxopenid && $user->wxopenid = $wxopenid;

        $height && $user->height = $height;
        $age && $user->age = $age;
        $weight && $user->weight = $weight;

        if ($mobile && $user::get(['mobile'=>$mobile])){
            return $user::get(['mobile'=>$mobile]);
        }
        if ($qqopenid && $user::get(['qqopenid'=>$qqopenid])){
            return $user::get(['qqopenid'=>$qqopenid]);
        }
        if ($wxopenid && $user::get(['wxopenid'=>$wxopenid])){
            return $user::get(['wxopenid'=>$wxopenid]);
        }

        if ($user->save()){
            return array("id"=>$user->id);
        }else{
            return 100;
        }


    }

    /**
     * 发送验证码
     */
    public function sendVerifyCode(){
        $code = Request::instance()->post('code');
        $mobile = Request::instance()->post('mobile');
        $text = "【".Config::get('yum_company')."】您的验证码是".$code."。如非本人操作，请忽略本短信";
        $rs = sendCode($mobile,$text);

        $result = json_decode($rs,true);
        if ($result['code']==0){
            return array();
        }else{
            return 100;
        }
    }


}
